<?php

error_reporting(0); // Set E_ALL for debuging

include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderConnector.class.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinder.class.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeDriver.class.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeLocalFileSystem.class.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderExProcessImage.php';

/**
 * Caculate dir path
 */
session_name('ledatcms');
session_start();

if ( (isset($_SESSION['file_manager']))
  && (! empty($_SESSION['file_manager']))
) {
    $basePath = $_SESSION['file_manager']['base_path'];
    $url      = $_SESSION['file_manager']['base_url'];
} else {
    echo 'error...';
    exit(0);
}

/**
 * Simple function to demonstrate how to control file access using "accessControl" callback.
 * This method will disable accessing files/folders starting from  '.' (dot)
 *
 * @param  string  $attr  attribute name (read|write|locked|hidden)
 * @param  string  $path  file path relative to volume root directory started with directory separator
 * @return bool|null
 **/
function access($attr, $path, $data, $volume) {
	return strpos(basename($path), '.') === 0       // if file/folder begins with '.' (dot)
		? !($attr == 'read' || $attr == 'write')    // set read+write to false, other (locked+hidden) set to true
		:  null;                                    // else elFinder decide it itself
}

function resizeImage($cmd, $result, $args, $elfinder) {
    if (isset($result['added']) && is_array($result['added'])) {
        global $basePath;
        $dir = __DIR__ . '/' . $basePath;
        $objImage = new elFinderExProcessImage($_SESSION['file_manager']);
        $objImage->config = $_SESSION['file_manager'];

        foreach ($result['added'] as $item) {
            $path = $dir . $item['name'];
            $objImage->exResize($path);
        }
        return TRUE;
    } else {
        return FALSE;
    }
}

$opts = array(
	// 'debug' => true,
    'bind' => array(
        'upload' => 'resizeImage'
    ),
	'roots' => array(
		array(
            'driver'          => 'LocalFileSystem',
            'path'            => $basePath,
            'URL'             => $url,
            'accessControl'   => 'access',
            'tmbPath'         => '.thumb',
            'uploadOverwrite' => false,
            'uploadMaxSize'   => '10M',
            'uploadOrder'     => 'allow, deny',
            'uploadAllow'     => array(
                'image/jpeg', 'image/png', 'image/gif',
                'application/pdf',
                'application/x-download',
                'application/zip',
                'multipart/x-zip',
                'application/x-zip-compressed',
                'application/x-shockwave-flash',
            ),
//            'attributes' => array(
//                array(
//                    'pattern' => '/^(.)+.(zip|pdf|swf)$/',
//                    'hidden'  => true,
//                ),
//            ),
        ),
    ),
);

// run elFinder
$connector = new elFinderConnector(new elFinder($opts));
$connector->run();

